package cn.com.dashihui.web.controller;

import java.io.IOException;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;

import cn.com.dashihui.web.base.BaseController;
import cn.com.dashihui.web.dao.WebNotice;
import cn.com.dashihui.web.service.WebNoticeService;

@RequiresAuthentication
public class WebNoticeController extends BaseController{
	private WebNoticeService service = new WebNoticeService();

	@RequiresPermissions("web:notice:index")
    public void index(){
        render("index.jsp");
    }

	public void page(){
		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		renderResult(0,service.findByPage(pageNum, pageSize));
	}

	public void toAdd(){
		render("add.jsp");
	}
	
	/**
	 * 添加
	 * @return -1：异常，0：成功，1：标题为空，2：标题过长，3：内容为空，4：图片上传失败
	 */
	public void doAdd(){
		//类型
		int type = getParaToInt("type");
		//标题
		String title = getPara("title");
		//概要
		String describe = getPara("describe");
		//是否展示
		int show = getParaToInt("show");
		//消息内容
		String detail = getPara("detail");
		//内容
		String link = getPara("link");
		WebNotice notice = new WebNotice()
			.set("title", title)
			.set("describe", describe)
			.set("type",type )
			.set("show",show )
			.set("detail", detail)
			.set("link", link);
		service.addNotice(notice);

		renderSuccess();
	}
	/**
	 * 编辑系统消息
	 */
	public void toEdit(){
		int id = getParaToInt(0,0);
		setAttr("object", service.findById(id));
		render("edit.jsp");
	}
	/**
	 * 更新
	 * @return -1：异常，0：成功，1：标题为空，2：标题过长，3：内容为空，4：图片上传失败
	 */
	public void doEdit(){
		int id = getParaToInt("noticeid");
		//类型
		int type = getParaToInt("type");
		//标题
		String title = getPara("title");
		//概要
		String describe = getPara("describe");
		//是否展示
		int show = getParaToInt("show");
		//消息内容
		String detail = getPara("detail");
		//内容
		String link = getPara("link");
		WebNotice notice = service.findById(id);
			notice.set("title", title)
			.set("describe", describe)
			.set("type",type )
			.set("show",show )
			.set("detail", detail)
			.set("link", link);
			service.editNotice(notice);
		 renderSuccess();
	}
	
	/**
	 * 删除
	 * @return -1：删除失败，0：删除成功
	 */
	public void doDelete(){
		int id = getParaToInt(0,0);
		if(id!=0){
			WebNotice notice = service.findById(id);
			//先判断如果上传了图片，则删除图片
			if(!StrKit.isBlank(notice.getStr("thumb"))){
				try {
					deleteFromFtp(notice.getStr("thumb"));
				} catch (IOException e) {
					logger.error("删除时，相关图片"+notice.getStr("thumb")+"删除失败！");
					e.printStackTrace();
				}
			}
			//然后再对记录进行删除
			notice.delete();
//			logger.info("删除公告【"+id+"】，及其图片");
			renderSuccess();
			return;
		}
		renderFailed();
	}
	
	/**
	 * 文本编辑中图片上传
	 */
	/*public void uploadimg(){
		UploadFile thumb = getFile();
		Map<String,Object> map = new HashMap<String,Object>();  
		if(thumb!=null){
			String thumbFileName;
			String dir = DirKit.getDir(DirKit.NOTICE);
			try {
				thumbFileName = uploadToFtp(dir,thumb);
				map.put("error", 0);  
				map.put("url",PropKit.get("constants.ftppath").concat(dir).concat(thumbFileName));  
				renderJson(map);
		        return;
			} catch (IOException e) {
				e.printStackTrace();
				logger.debug("上传图片失败");
				map.put("error", 1);  
				map.put("message","图片上传失败");  
				renderJson(map);
			}
		}else{
			map.put("error", 1);  
			map.put("message","图片为空");  
			renderJson(map);
		}
	}*/
}
